/**
 * 阅读查找 入口
 * Created by alice on 2016/7/15.
 */
require.config({
    baseUrl:'../js',
    paths:{
        "getData":"Util/getData",
        "config":"common/config",
        "components":"common/components",
        "common":"common/common",
        "jquery":"lib/jquery.min",
        "wx":"lib/jweixin-1.0.0"
    },
    shim : {
        "components": { deps : ["jquery"] }
    }
});
require(['jquery','getData','config','common','components','wx'],function($,getData,config,common,components,wx){
    //清除默认input焦点
    $(".receive_input input").blur();
    var gm=common.gm;
    //获取 参数
    var token=gm.para.get(window.location.href,"token");
    var _name=gm.para.get(window.location.href,"name");
    var _contentCategoryUuid=gm.para.get(window.location.href,"contentCategoryUuid");
    //转码
    _name= decodeURIComponent(_name);
    $(".receive_input input").val(_name);
    // 加载后执行接口

    getData.getJson({
        page:"content",
        inter:"getArticleInfoList",
        data:{token:token,contentType:_contentCategoryUuid,pageSize:15,pageNum:1,collectRule:0,contentTitle:_name||"",illnessId:""},
        callback:function(data){
            getRead(data);
        }
    });

    //为按钮绑定事件
    $(".receive_input .start_find").bind({
        touchend:function(){
            var _n=$(".receive_input input").val();
            // 加载后执行接口
            getData.getJson({
                page:"content",
                inter:"getArticleInfoList",
                data:{token:token,contentType:_contentCategoryUuid,pageSize:15,pageNum:1,collectRule:0,contentTitle:_n||"",illnessId:""},
                callback:function(data){
                    getRead(data);
                }
            });
        }
    });
     //回调接口
    function getRead(data){
        if(!data){
            $.alert("服务器出错!");
        }
        if(data.returnStatus!=undefined && !data.returnStatus){
            $("#search_empty").show();
        }else
            $("#search_empty").hide();
        if(data.code=="2000"){
            $(".gsid_2 ul").html(bindData(data.value)).collectnews();
            //$(".scrollLoading").scrollLoading({container: $(".gsid_2 ul")});
            isOpenUrl();
            $(".redmyu").show();
        }else{
            $.alert(data.message);
        }
    }
    //填充数据
    function bindData(data){
        var arr = [];
        var _isy = (window.location.href.indexOf("#b") > 0);
        var tmp = '<li class="{contentType} isopen" contentUuid="{contentUuid}" favid="{favoriteUuid}">\
                    <div class="asbg">\
                        <div class="live">\
                            <a href="{url}"><img style="background:url("../imgs/loading.gif") no-repeat center;" class="lazy scrollLoading" src="{imageUrl}"></a>\
                        </div>\
                    <div class="text">\
		                <div class="collectionContentTitle"><p>{contentTitle}</p></div>\
                        <p class="time">\
                            {collectNum}{readNum}\
                        </p>\
                    </div>\
                    <div class="clear">\
                    </div>\
                    </li>';
        $(data).each(function(i,o){
            var imgstr=o.imageUrl;
            o.imageUrl = gm.common.isObjUsable(imgstr)?(imgstr.substr(0,7)=="http://"?imgstr:window._interfaceImgPath+""+imgstr):"/static/imgs/spimg.png";

            //是否收藏
            var isCollection=o.collect == true?1:0;
            var title= o.contentTitle;
            var desc= o.contentTitle;
            var img= o.imageUrl;
            var favoriteUuid= o.id;

            var str="isCollection="+isCollection+"&title="+title+"&desc="+desc+"&img="+img+"&favoriteUuid="+favoriteUuid+"&token="+token+"&collectRule=0";

            o.url = o.contentType=="3"?o.contentUrl+"&"+str:'informationfordetails.html?client=ios&'+str;
            var readNum= o.readNum;
            if(!gm.common.isObjUsable(readNum))
                o.readNum="<span style='margin-right:.5rem;'>阅读 0</span>";
            else
                o.readNum="<span style='margin-right:.5rem;'>阅读 "+readNum+"</span>";
            var collectNum= o.collectNum;
            if(!gm.common.isObjUsable(collectNum))
                o.collectNum="<span style='margin-right:.5rem;'>收藏 0</span>";
            else
                o.collectNum="<span style='margin-right:.5rem;'>收藏 "+collectNum+"</span>";

            arr.push(gm.replace(tmp,o));
        });
        return arr.join("");
    }
    //防止表单提交
    $('form').on('submit', function(e){
        // 不提交
        return false;
    });
    function isOpenUrl(){
        $(".isopen").bind({
            touchend:function(e){
                e.preventDefault();
            }
        });
        $(".isopen").ontouch(function(){
                var href = $(this).find("a").attr("href");
                gm.pul.toUrl(href);
            },true
        );
    }

});
